package gameserver.model.gameobjects;

import java.sql.Timestamp;

import gameserver.dataholders.DataManager;
import gameserver.model.templates.item.ItemTemplate;

import org.apache.log4j.Logger;

import com.sun.org.apache.xerces.internal.xs.ItemPSVI;

public class Item extends BaseObject
{
	private static Logger log = Logger.getLogger(Item.class);

	private int itemId;

	private long itemCount = 1;

	private ItemTemplate itemTemplate;

	private boolean isEquipped;

	private PersistentState persistentState;

	private Timestamp expireTime = null;

	private int slot;

	public Item(int objectId, int itemId, ItemTemplate itemTemplate,
			long itemCount, boolean isEquipped, int slot)
	{
		super(objectId);
		this.itemId = itemId;
		this.itemTemplate = itemTemplate;
		this.itemCount = itemCount;
		this.isEquipped = isEquipped;
		this.slot = slot;
		if (itemTemplate.getExpireMins() > 0)
			this.expireTime = new Timestamp(System.currentTimeMillis()
					+ itemTemplate.getExpireMins() * 60 * 1000);
		this.persistentState = PersistentState.NEW;
	}

	public Item(int objectId, int itemId, long itemCount, boolean isEquipped,
			int slot, Timestamp expireTime)
	{
		super(objectId);
		this.itemTemplate = DataManager.ITEM_DATA.getItemTemplate(itemId);
		this.itemId = itemId;
		this.itemCount = itemCount;
		this.isEquipped = isEquipped;
		this.slot = slot;
		this.expireTime = expireTime;
	}

	public int getItemId()
	{
		return itemTemplate.getTemplateId();
	}

	@Override
	public String getName()
	{
		// TODO Auto-generated method stub
		return itemTemplate.getName();
	}

	public ItemTemplate getItemTemplate()
	{
		if (itemTemplate == null)
			log
					.warn("Item was not populated correctly. Item template is missing"
							+ ", ItemObjectId: "
							+ getObjectId()
							+ ", ItemId: "
							+ itemId);
		return itemTemplate;
	}

	public void setItemTemplate(ItemTemplate itemTemplate)
	{
		this.itemTemplate = itemTemplate;
	}

	public boolean isEquipped()
	{
		return isEquipped;
	}

	/**
	 * @param isEquipped
	 *            the isEquipped to set
	 */
	public void setEquipped(boolean isEquipped)
	{
		this.isEquipped = isEquipped;
		setPersistentState(PersistentState.UPDATE_REQUIRED);
	}

	public long getItemCount()
	{
		return itemCount;
	}

	/**
	 * @param itemCount
	 *            the itemCount to set
	 */
	public void setItemCount(long itemCount)
	{
		this.itemCount = itemCount;
		setPersistentState(PersistentState.UPDATE_REQUIRED);
	}

	public int getSlot()
	{
		return slot;
	}

	public void setSlot(int slot)
	{
		this.slot = slot;
	}

	public void setExpireTime(Timestamp expireTime)
	{
		this.expireTime = expireTime;
		setPersistentState(PersistentState.UPDATE_REQUIRED);
	}

	/**
	 * @return the expireTime
	 */
	public Timestamp getExpireTime()
	{
		return expireTime;
	}

	public PersistentState getPersistentState()
	{
		return persistentState;
	}

	public void setPersistentState(PersistentState persistentState)
	{
		switch (persistentState)
		{
		case DELETED:
			break;
		case UPDATE_REQUIRED:
			if (this.persistentState == PersistentState.NEW)
				break;
		default:
			this.persistentState = persistentState;
		}
	}
}
